//
//  TheTopView.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/9/27.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "TheTopView.h"
#import "Masonry.h"
#import "UIColor+CL.h"
#import "ZJLoopScrollView.h"
#import "BaseAllViewController.h"
#define STYLE0 @"0" // 主项目风格
#define STYLE1 @"1" // 学员端风格

@implementation TheTopView{
    ZJLoopScrollView *loopView;
    
}



-(void)WithStyle:(NSString *)style andDelegate:(id)delegate{
    
    _delegate = delegate;
    //    _controller = (UIViewController *)delegate;
    
    //    self = [super init];
    //    if(self){
    self.backgroundColor = [UIColor redColor];
    
    //        [_controller.view addSubview:self];
    
    if([style isEqualToString:STYLE0]){
        
        [self style];
    }else{
        [self style1];
    }
    //    }
    //    return self;
}

-(void)style{
    
    //这里添加功能判断
    if (getAvatarUpload()) {
        NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(changeImageView:) name:@"NeedChangeImageViewImage" object:nil];
    }else{
        NSLog(@"没有！！！");
    }

    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *StuName = [ud objectForKey:@"username"];
    NSString *kindStr = [ud objectForKey:@"roleNames"];
    if (StuName) {
        self.topBgNameLabel.text = StuName;
    }else{
        self.topBgNameLabel.text = @"李小明";
    }
    //     = @"李小明";
    if (kindStr.length>0) {
        self.topBgKindLabel.text = [NSString stringWithFormat:@"用户角色：%@",kindStr];
        
    }else{
        self.topBgKindLabel.text = @"用户角色：教务老师";
    }
    
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"often_into_small"]];//右侧小箭头
    [self.topBgView addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topBgView.mas_centerY);
        make.right.equalTo(self.topBgView.mas_right).offset(-36/2);
    }];
    
    
    iv.hidden = YES;
    
    
    
}

-(UIImageView *)topBgView{ // 蓝色背景
    if (!_topBgView) {
        _topBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"edus_portrait_bg"]];
        [self.topView addSubview:_topBgView];
        [_topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topView.mas_top);
            make.left.equalTo(self.topView.mas_left);
            make.right.equalTo(self.topView.mas_right);
            make.height.equalTo(@(120/2));
        }];
    }
    
    
    return _topBgView;
}


-(UIImageView *)topBgIconView{ // 头像 Img
    if (!_topBgIconView) {
        _topBgIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"edus_portrait_normal"]];
        [self.topBgView addSubview:_topBgIconView];
        [_topBgIconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.topView.mas_left).offset(36/2);
            make.centerY.equalTo(self.topBgView.mas_centerY);
            make.height.equalTo(@(40));
            make.width.equalTo(@(40));
        }];
        _topBgIconView.layer.masksToBounds = YES;
        _topBgIconView.layer.cornerRadius = _topBgIconView.frame.size.width / 2;
    }
    return _topBgIconView;
}

-(UILabel *)topBgNameLabel{ //  姓名
    if (!_topBgNameLabel) {
        _topBgNameLabel = [[UILabel alloc] init];
        [self.topBgView addSubview:_topBgNameLabel];
        _topBgNameLabel.textColor = [UIColor colorWithHex:0xffffff];
        [_topBgNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.topBgView.mas_centerY);
            //这里添加功能判断
            if (!getUserRoleName()) {
//            if ([getUserType() isEqualToString:UserISHPSTeacher]) {
//汉普森客户因为取消用户角色而将以前的布局取消，变成名字最多显示到屏幕最右侧，（以前是一般多10分之一）
                make.right.equalTo(self.topView.mas_right);
            }else{
                make.right.equalTo(self.topView.mas_centerX).offset(KWIDTH/10);
            }


            
            
            make.left.equalTo(self.topBgIconView.mas_right).offset(20/2);
            
        }];
        _topBgNameLabel.font = [UIFont systemFontOfSize:32/2];
    }
    return _topBgNameLabel;
}

-(UILabel *)topBgKindLabel{ //  用户角色
    if (!_topBgKindLabel) {
        _topBgKindLabel = [[UILabel alloc] init];
        _topBgKindLabel.textColor = [UIColor colorWithHex:0x1f7b8c];
        _topBgKindLabel.font = [UIFont systemFontOfSize:30/2];
        [self.topBgView addSubview:_topBgKindLabel];
        [_topBgKindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.topBgView.mas_centerY);
            make.left.equalTo(self.topView.mas_centerX).offset(KWIDTH/10);
            make.right.equalTo(self.topView.mas_right).offset(-(36)/2);
        }];
        _topBgKindLabel.textAlignment = NSTextAlignmentRight;
    }
    //汉普森客户不需要此字段，故隐藏
    //这里添加功能判断
    if (!getUserRoleName()) {
        _topBgKindLabel.hidden = YES;
    }else{
        _topBgKindLabel.hidden = NO;
    }

    
    return _topBgKindLabel;
}





-(UIView *)topView{ //  整个 视图 父试图
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor colorWithHex:0xf5f8fa];
        
        
        [self addSubview:_topView];
        _topView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 134/2);
        
        
    }
    return _topView;
}


-(void)style1{
    //    self.topView;😂
    
    //    loopView = [[ZJLoopScrollView alloc] init];
    //    loopView.autoScroll = YES;
    //    loopView.imageArray = @[@"stu_see_banner",@"stu_see_banner",@"stu_see_banner"];
    //
    //
    //    [self addSubview:loopView];
    //    loopView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300/2);
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KWIDTH, 150) shouldInfiniteLoop:YES imageNamesGroup:@[@"scrollViewBG1",@"scrollViewBG2",@"scrollViewBG3"]];
    _cycleScrollView.delegate = self.delegate;
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    [self addSubview:_cycleScrollView];
    _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _cycleScrollView.autoScrollTimeInterval = 4.0f;
    
}


- (void)changeImageView:(NSNotification *)sender{
    NSLog(@"%@",sender);
    
    NSLog(@"jslxyyxyyxyyxyyxyyxyyxyy");
    NSString *str = [sender object];
    if ([str isEqualToString:@"YES"]) {
        NSLog(@"grhslxyyxyyxyyxyyxyyxyyxyy");
//        self.topBgIconView.image = [self getLocalIcon];
        UIImage *img = [self getLocalIcon];
        
        self.topBgIconView.image = img;
    }else{
        self.topBgIconView.image = [UIImage imageNamed:@"my_portrait_normal"];
    }
}

- (UIImage *)getLocalIcon{
    if (userIcon()) {
        _imagePath = userIcon();
    }else{
        return [UIImage imageNamed:@"默认头像"];
    }
    
    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"%@",paths);
    NSRange range = [_imagePath rangeOfString:@"Documents"];
    NSRange needRange = NSMakeRange(0, range.location+range.length);
    NSLog(@"%@",_imagePath);
    if (range.location != NSNotFound) {
        _imagePath = [_imagePath stringByReplacingCharactersInRange:needRange withString:paths];//每次更换沙盒路径需要重新替换新的路径，否则找不到文件。
    }
    
    NSLog(@"%@",_imagePath);
    
    UIImage *photoImg = [[UIImage alloc]initWithContentsOfFile:_imagePath];
    return photoImg;
    
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
